package org.example.gc;

/**
 *
 * @author youshang
 * @date 2022/03/30 21:49
 **/
public class LocalVarGC {
    public void localVarGC1(){
        byte[] bytes = new byte[10 * 1024 * 1024];
        System.gc();
        /*
        对象没有被回收掉
        Young GC 回收了一次没有回收掉，触发了Full GC 但是还是没有回收掉

[GC (System.gc()) [PSYoungGen: 15493K->10736K(76288K)] 15493K->10964K(251392K), 0.0074232 secs] [Times: user=0.05 sys=0.01, real=0.01 secs]
[Full GC (System.gc()) [PSYoungGen: 10736K->0K(76288K)] [ParOldGen: 228K->10864K(175104K)] 10964K->10864K(251392K), [Metaspace: 3025K->3025K(1056768K)], 0.0050912 secs] [Times: user=0.01 sys=0.01, real=0.00 secs]
         */
    }

    public void localVarGC2(){
        byte[] bytes = new byte[10 * 1024 * 1024];
        bytes = null;
        System.gc();
        /*
[GC (System.gc()) [PSYoungGen: 15493K->800K(76288K)] 15493K->808K(251392K), 0.0009636 secs] [Times: user=0.01 sys=0.00, real=0.00 secs]
[Full GC (System.gc()) [PSYoungGen: 800K->0K(76288K)] [ParOldGen: 8K->625K(175104K)] 808K->625K(251392K), [Metaspace: 3053K->3053K(1056768K)], 0.0034328 secs] [Times: user=0.02 sys=0.00, real=0.01 secs]

        对象被回收了
         */
    }

    public void localVarGC3(){
        {
            byte[] bytes = new byte[10 * 1024 * 1024];
        }
        System.gc();
        /*
[GC (System.gc()) [PSYoungGen: 15493K->10720K(76288K)] 15493K->10980K(251392K), 0.0053415 secs] [Times: user=0.05 sys=0.01, real=0.00 secs]
[Full GC (System.gc()) [PSYoungGen: 10720K->0K(76288K)] [ParOldGen: 260K->10865K(175104K)] 10980K->10865K(251392K), [Metaspace: 3055K->3055K(1056768K)], 0.0047761 secs] [Times: user=0.01 sys=0.01, real=0.01 secs]

        对象没有被回收，此时涉及到了slot 的复用
         */
    }
    public void localVarGC4(){
        {
            byte[] bytes = new byte[10 * 1024 * 1024];
        }
        int value = 10;
        System.gc();
        /*
[GC (System.gc()) [PSYoungGen: 15493K->864K(76288K)] 15493K->872K(251392K), 0.0014179 secs] [Times: user=0.01 sys=0.01, real=0.00 secs]
[Full GC (System.gc()) [PSYoungGen: 864K->0K(76288K)] [ParOldGen: 8K->625K(175104K)] 872K->625K(251392K), [Metaspace: 3015K->3015K(1056768K)], 0.0038259 secs] [Times: user=0.02 sys=0.00, real=0.01 secs]

        对象被回收了

         */
    }

    public void localVarGC5(){
        localVarGC1();
        System.gc();
        /*
[GC (System.gc()) [PSYoungGen: 15493K->10720K(76288K)] 15493K->10976K(251392K), 0.0064406 secs] [Times: user=0.05 sys=0.00, real=0.01 secs]
[Full GC (System.gc()) [PSYoungGen: 10720K->0K(76288K)] [ParOldGen: 256K->10865K(175104K)] 10976K->10865K(251392K), [Metaspace: 3036K->3036K(1056768K)], 0.0047358 secs] [Times: user=0.01 sys=0.01, real=0.00 secs]
[GC (System.gc()) [PSYoungGen: 1310K->64K(76288K)] 12175K->10929K(251392K), 0.0003556 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[Full GC (System.gc()) [PSYoungGen: 64K->0K(76288K)] [ParOldGen: 10865K->631K(175104K)] 10929K->631K(251392K), [Metaspace: 3036K->3036K(1056768K)], 0.0035217 secs] [Times: user=0.02 sys=0.00, real=0.01 secs]

        对象被回收了
         */
    }

    public static void main(String[] args) {
        LocalVarGC local = new LocalVarGC();
        local.localVarGC5();
    }
}
